package cn.fl.spl.service.impl;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.IStoreStatsDataService;
import cn.fl.spl.transform.StoreStatsDataVOTransform;
import cn.fl.spl.vo.StoreStatsDataVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.mongo.facade.analysis.api.IStoreStatsDataApiService;
import fl.mongo.facade.analysis.api.IStoreStatsDataQueryApiService;
import fl.mongo.facade.analysis.condition.StoreStatsDataCondition;
import fl.mongo.facade.analysis.dto.StoreStatsDataDTO;
import fl.mongo.facade.base.page.PaginationVo;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.condition.SplInfoQueryCondition;
import fl.spl.facade.spl.dto.SplInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jiangzhongge on 2018-3-2.
 * Time 11:35.
 * Class belongs to fl-ass-service.
 */
@Service
public class StoreStatsDataServiceImpl implements IStoreStatsDataService {

    @Autowired
    private IStoreStatsDataApiService storeStatsDataApiService ;

    @Autowired
    private ISplInfoApiService splInfoApiService ;

    @Autowired
    private ISysUsrApiService sysUsrApiService ;

    @Autowired
    private IStoreStatsDataQueryApiService storeStatsDataQueryApiService ;

    @Override
    public List<StoreStatsDataVO> getStoreStatsDataList(StoreStatsDataCondition condition) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        return StoreStatsDataVOTransform.toVOList(this.storeStatsDataQueryApiService.getStoreStatsDataList(condition));
    }

    @Override
    public int insertStoreStatsData(StoreStatsDataVO storeStatsDataVO) throws BaseException {
        int result = 0 ;
        if(null == storeStatsDataVO){
            BaseLogUtils.newWebLogger("参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        }else{
            result = this.storeStatsDataApiService.addStoreStatsData(StoreStatsDataVOTransform.toDTO(storeStatsDataVO));
        }
        return result;
    }

    @Override
    public boolean modfiyStoreStatsData(StoreStatsDataCondition condition, StoreStatsDataVO storeStatsDataVO) throws BaseException {
        boolean flag = false ;
        if(null == condition || null == storeStatsDataVO){
            BaseLogUtils.newWebLogger("修改接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        }else{
            flag = this.storeStatsDataApiService.updateStoreStatsData(condition,StoreStatsDataVOTransform.toDTO(storeStatsDataVO));
        }
        return flag;
    }

    @Override
    public boolean upsertStoreStatsData(StoreStatsDataCondition condition, StoreStatsDataVO storeStatsDataVO) throws BaseException {
        boolean flag = false ;
        if(null == condition || null == storeStatsDataVO){
            BaseLogUtils.newWebLogger("更新插入接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        }else{
            flag = this.storeStatsDataApiService.upsertStoreStatsData(condition, StoreStatsDataVOTransform.toDTO(storeStatsDataVO));
        }
        return flag;
    }

    @Override
    public StoreStatsDataVO getOneStoreStatsData(StoreStatsDataCondition condition) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("查询数据接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        return StoreStatsDataVOTransform.toVO(this.storeStatsDataQueryApiService.getOneStoreStatsData(condition));
    }

    @Override
    public PaginationVo getStoreStatsDataListByPage(PaginationVo paginationVo) throws BaseException {
        if(null == paginationVo){
            BaseLogUtils.newWebLogger("分页查询数据接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        PaginationVo page = this.storeStatsDataQueryApiService.findStoreStatsDataListByPage(paginationVo);
        List<StoreStatsDataDTO> datas = (List<StoreStatsDataDTO>)page.getDatas();
        List<StoreStatsDataVO> voList = StoreStatsDataVOTransform.toVOList(datas);
        if(CollectionUtils.isNotEmpty(voList)){
        	ArrayList<Long> splIds = new ArrayList<>();
        	Map<Long,SplInfoDTO> map = new HashMap<>();
        	for (StoreStatsDataVO vo : voList) {
				splIds.add(vo.getSplId());
			}
        	SplInfoQueryCondition condition = new SplInfoQueryCondition();
        	condition.setIds(splIds);
			List<SplInfoDTO> splInfoList = splInfoApiService.searchSplInfoList(condition);
			if(CollectionUtils.isNotEmpty(splInfoList)){
				for (SplInfoDTO splInfoDTO : splInfoList) {
					map.put(splInfoDTO.getId(), splInfoDTO);
				}
				
				for (StoreStatsDataVO vo : voList) {
					SplInfoDTO splInfoDTO = map.get(vo.getSplId());
					if(splInfoDTO != null){
						vo.setSplNm(splInfoDTO.getSplNm());
					}
				}
			}
        }
        
        page.setDatas(voList);
        return page ;
    }

    @Override
    public Long statisStoreStatsDataCount(StoreStatsDataCondition condition) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("查询数据统计参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        return this.storeStatsDataQueryApiService.queryStoreStatsDataCount(condition);
    }

    @Override
    public List<StoreStatsDataVO> queryAggregationDataInfo(StoreStatsDataCondition condition, String groupType, String groupParam, String aliasParam, String projectType, String sortType, String sortParam, String... fields) {
        return StoreStatsDataVOTransform.toVOList(this.storeStatsDataQueryApiService.queryAggregationDataInfo(condition,groupType,groupParam,aliasParam,projectType,sortType,sortParam,fields));
    }

    @Override
    public DataResultVO exportStoreStatsRecList(StoreStatsDataCondition condition) throws BaseException {

        return null;
    }

	@Override
	public PaginationVo getStoreStatsListByPage(PaginationVo pager,String isExport) throws BaseException {
		// TODO Auto-generated method stub
		if(pager == null){
            BaseLogUtils.newWebLogger("参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
			return null ;
		}
		PaginationVo page = this.storeStatsDataQueryApiService.searchStoreStatsListByPage(pager,isExport);
		List<StoreStatsDataDTO> datas = (List<StoreStatsDataDTO>)page.getDatas();
        List<StoreStatsDataVO> voList = StoreStatsDataVOTransform.toVOList(datas);
        BaseLogUtils.newWebLogger("获取数据成功").setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .setKeys("voList", JSON.toJSONString(voList))
                .info();
        if(CollectionUtils.isNotEmpty(voList)){
        	List<Long> splIds = new ArrayList<>();
        	List<Long> usrIds = new ArrayList<>();
        	Map<Long,SplInfoDTO> map = new HashMap<>();
        	Map<Long,SysUsrDTO> usrMap = new HashMap<>();
        	for (StoreStatsDataVO vo : voList) {
        		if(StringUtils.isNotEmpty(vo.getSplId())){
					splIds.add(vo.getSplId());
					usrIds.add(Long.valueOf(vo.getId()));
        		}
			}
        	//获取供应商信息
        	
        	SplInfoQueryCondition condition = new SplInfoQueryCondition();
        	condition.setIds(splIds);
        	// 获取供应商信息
			List<SplInfoDTO> splInfoList = splInfoApiService.searchSplInfoList(condition);
			//获取系统联系人信息
			SysUsrQueryCondition condition1 = new SysUsrQueryCondition();
	        condition1.setUsrIds(usrIds);
	        condition1.setSysCd(CurrentThreadContext.getCurrentSysCd());
			List<SysUsrDTO> usrList = sysUsrApiService.searchSysUsrList(condition1 );
			
			if(CollectionUtils.isNotEmpty(splInfoList)){
				for (SplInfoDTO splInfoDTO : splInfoList) {
					map.put(splInfoDTO.getId(), splInfoDTO);
				}
				for (SysUsrDTO sysUsrDTO : usrList) {
					usrMap.put(sysUsrDTO.getId(), sysUsrDTO);
				}
				for (StoreStatsDataVO vo : voList) {
					SplInfoDTO splInfoDTO = map.get(vo.getSplId());
					if(splInfoDTO != null){
						vo.setSplNm(splInfoDTO.getSplNm());
					}
					SysUsrDTO sysUsrDTO = usrMap.get(Long.valueOf(vo.getId()));
					if(sysUsrDTO != null){
						vo.setTel(sysUsrDTO.getMp());
						if(StringUtils.isBlank(sysUsrDTO.getRefCertNo())){
							vo.setIsAutonym(0);
						}else{
							vo.setIsAutonym(1);
						}
					}
				}
			}
        }
        
        page.setDatas(voList);
		return page;
	}
	
	
	@Override
	public Long searchActiveUsrCount(StoreStatsDataCondition condition) throws BaseException {
		// TODO Auto-generated method stub
		return this.storeStatsDataQueryApiService.searchActiveUsrCount(condition);
	}

}